package com.cloud.pole.alipay;

import java.io.File;
import java.io.IOException;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.AntCertificationUtil;
import com.alipay.api.internal.util.file.FileUtils;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;

public class PayTestMain {

	public PayTestMain() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String args[]) {
		PayTestMain genCode = new PayTestMain();
		try {
			genCode.sandTran();
//			genCode.readSnInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//读取app公匙序列号 和 根证书序列号
	private void readSnInfo() {
		try {
			System.out.println("app_cert_sn=" + AlipaySignature.getCertSN("D:/crt/appCertPublicKey_2021001144603410.crt"));
			String rootCertContent = FileUtils.readFileToString(new File("D:/crt/alipayRootCert.crt"));
			System.out.println("alipay_root_cert_sn=" + AntCertificationUtil.getRootCertSN(rootCertContent));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	//转账功能实现,
	//换成正式地址及配置一样使用
	private void sandTran() throws AlipayApiException, IOException {
		String appId = "2021001144603410";
		//String sand_pro_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCT2dJlCu1vk5t1RTfUuXA95CsW7uiN2Qax6b/3qhgvdiMPfkqW+EV8bNp6lrrrh2n8aQh1aeO1IRmutOnEoj+x3EUWKXh02gTqTuGSWIG27w8Rwfx7qs5fp9/2K+fp7mqD7pS0ClPWakdgFkT4cAgRqHnHBk0idenw1KKErOUMKJ1qGbByZOUU9m8T07sXo5lCfgRUbOoi+xob+15GrPh1k4lp8asVZsSfHipySg2SkuueqBXSTGJX8vVVadvZgbTFGMdMwa3DZMwN7yYwl5YOCc7umAlvevqXqyf9otat5g43sdyi81q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9yZL8lK9nwKBgQC86JsnEp26Zz1QgzHJgnk+yDF5YM5T8tndImpqFArlsKxDL04QphFl5/iEtM8YvAxugScP7eBbrYVdJoPnVyEYQP7NlJ0bLxc8Yw4MOiwW7Ar8ky9whM8jsrkzpGlckVkTiCZTvxdfjDpa8m9QFw4tlBSipGT8SU50dyoD4geXwQKBgQDKF4zKAIt4bV98vdVzvtBs6avgAADADWn0H7RSvhtRLFyBrkGta9x7HShVc3sH4wesHfHDNNY/am5D8R3X/RHwVsH3IrfQYtSgnmPAN5yER/2FFzYuNgG2522whGgeA5ZZhrK4SMevVOcoJykE4ms0PYWhWhLamZe1idhwtboqHwKBgAv/Mas/kWi/GbO9QTIs6QpFqG6u6SuHprJWdy+Zaj/AApCZtnl0MnKoUq4K1YQanSd/aD2ou7DWyjtqMMJGVEG4DvZ7U6bgTLd3CvNX1mj2QJRdvIuGw6jetshCzrYkPOXypYx3/EIDI8mWz97kN/BJES5rBkbHWQYoczCf8B+BAoGBAI5mYZn5wVZkl++x8Zb5KzIEnbVrnXTAPlHcYO5RNesJ1Hk7xuvfK2nS22XfxSveZMIXE9gxVUKJAB+FKuknQ37HFvxrRlZjZTRcDg+uk5s8mDIgrtCLLrKo3hDjBXnQ6bR59j7vls749QWuuyDmQdIuVyqf16go9wQOmr9cSNzh";
		CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
		// 这个是沙箱环境的地址
		// 正式的是 https://openapi.alipay.com/gateway.do
		certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
		certAlipayRequest.setAppId(appId);
		certAlipayRequest.setPrivateKey(sand_pro_key);
		certAlipayRequest.setFormat("json");
		certAlipayRequest.setCharset("UTF-8");
		certAlipayRequest.setSignType("RSA2");
		// 上传csr文件后 完成配置,支付宝后台能下载3个文件
		// 应用公匙
		certAlipayRequest.setCertPath("D:/crt/appCertPublicKey_2021001144603410.crt");
		// 支付宝公匙
		certAlipayRequest.setAlipayPublicCertPath("D:/crt/alipayCertPublicKey_RSA2.crt");
		// 支付宝 根 公匙
		certAlipayRequest.setRootCertPath("D:/crt/alipayRootCert.crt");

		DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

		// 这里测试的是转账功能 其他接口参考文档
		AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
		model.setOutBizNo("" + System.currentTimeMillis());
		model.setProductCode("TRANS_ACCOUNT_NO_PWD");
		model.setBizScene("DIRECT_TRANSFER");
		Participant participant = new Participant();
		participant.setIdentity("13755002297@139.com");
		participant.setIdentityType("ALIPAY_LOGON_ID");
		participant.setName("陈胜良");
		model.setPayeeInfo(participant);
		String totalAmount = "0.10";
		model.setTransAmount(totalAmount);
		model.setRemark("测试单笔转账到支付宝");

		AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
		request.setBizModel(model);

		// 这里必须添加 app公匙证书的序列号和阿里根证书的序列号
		// 获得方法 是 上面的 readSnInfo()
		
		request.putOtherTextParam("app_cert_sn", AlipaySignature.getCertSN("D:/crt/appCertPublicKey_2021001144603410.crt"));
		request.putOtherTextParam("alipay_root_cert_sn", AntCertificationUtil.getRootCertSN(FileUtils.readFileToString(new File("D:/crt/alipayRootCert.crt"))));

		AlipayFundTransUniTransferResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
	}

}
